package com.nbcio.modules.estar.in.service.impl;

import com.nbcio.modules.estar.in.entity.ErpIn;
import com.nbcio.modules.estar.in.entity.ErpInB;
import com.nbcio.modules.estar.in.mapper.ErpInBMapper;
import com.nbcio.modules.estar.in.mapper.ErpInMapper;
import com.nbcio.modules.estar.in.service.IErpInService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 入库单
 * @Author: nbacheng
 * @Date:   2024-04-02
 * @Version: V1.0
 */
@Service
public class ErpInServiceImpl extends ServiceImpl<ErpInMapper, ErpIn> implements IErpInService {

	@Autowired
	private ErpInMapper erpInMapper;
	@Autowired
	private ErpInBMapper erpInBMapper;
	
	@Override
	@Transactional
	public void saveMain(ErpIn erpIn, List<ErpInB> erpInBList) {
		erpInMapper.insert(erpIn);
		if(erpInBList!=null && erpInBList.size()>0) {
			for(ErpInB entity:erpInBList) {
				//外键设置
				entity.setInId(erpIn.getId());
				erpInBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ErpIn erpIn,List<ErpInB> erpInBList) {
		erpInMapper.updateById(erpIn);
		
		//1.先删除子表数据
		erpInBMapper.deleteByMainId(erpIn.getId());
		
		//2.子表数据重新插入
		if(erpInBList!=null && erpInBList.size()>0) {
			for(ErpInB entity:erpInBList) {
				//外键设置
				entity.setInId(erpIn.getId());
				erpInBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		erpInBMapper.deleteByMainId(id);
		erpInMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			erpInBMapper.deleteByMainId(id.toString());
			erpInMapper.deleteById(id);
		}
	}
	
}
